package org.alexisjago.footballmanager.DAO;

import java.util.List;
import java.sql.*;

import org.alexisjago.footballmanager.domain.Player;

public class PlayerJDBCDAO implements IPlayerDAO {
	
	private Connection connect = null;
	private PreparedStatement preparedStatement = null;
	private ResultSet resultSet = null;
	
	/* (non-Javadoc)
	 * @see org.alexisjago.footballmanager.DAO.IPlayerDAO#getPlayer(int)
	 */
	@Override
	public Player getPlayer(int playerID){
		
		Player player = new Player();
		try{
		    Class.forName("com.mysql.jdbc.Driver");
		    
			// Setup the connection with the DB
			connect = DriverManager.getConnection("jdbc:mysql://localhost/football?"
							+ "user=alex&password=password");

			// Statements allow to issue SQL queries to the database
			preparedStatement = connect.prepareStatement("select name, telephone, email from player where id = ?");

			preparedStatement.setInt(1, playerID);
			
			resultSet = preparedStatement.executeQuery();
			resultSet.next();
			
			player.setName(resultSet.getString("name"));
			player.setTelephone(resultSet.getString("telephone"));
			player.setEmail(resultSet.getString("email"));
		}
		catch(Exception e){
			System.out.println(e);
			
		}
		
		return player;
	}

	/* (non-Javadoc)
	 * @see org.alexisjago.footballmanager.DAO.IPlayerDAO#savePlayer(org.alexisjago.footballmanager.domain.Player)
	 */
	@Override
	public void savePlayer(Player player){

		try{
			Class.forName("com.mysql.jdbc.Driver");

			// Setup the connection with the DB
			connect = DriverManager.getConnection("jdbc:mysql://localhost/football?"
					+ "user=alex&password=password");
			// Statements allow to issue SQL queries to the database
			preparedStatement = connect.prepareStatement("insert into player (name, telephone, email) values (?,?,?)");

			preparedStatement.setString(1, player.getName());
			preparedStatement.setString(2, player.getTelephone());
			preparedStatement.setString(3, player.getEmail());

			preparedStatement.executeUpdate();
		}
		catch(Exception e){
			System.out.println(e);
		}
	}
	
	/* (non-Javadoc)
	 * @see org.alexisjago.footballmanager.DAO.IPlayerDAO#updatePlayer(org.alexisjago.footballmanager.domain.Player)
	 */
	@Override
	public void updatePlayer(Player player){
		
		try{
			Class.forName("com.mysql.jdbc.Driver");

			// Setup the connection with the DB
			connect = DriverManager.getConnection("jdbc:mysql://localhost/football?"
					+ "user=alex&password=password");
			// Statements allow to issue SQL queries to the database
            preparedStatement = connect.prepareStatement("update player set name = ?, telephone = ?, email = ? where id = ?");
			preparedStatement.setString(1, player.getName());
			preparedStatement.setString(2, player.getTelephone());
			preparedStatement.setString(3, player.getEmail());
			preparedStatement.setInt(4, player.getID());

			preparedStatement.executeUpdate();
		}
		catch(Exception e){
			System.out.println(e);
		}
	}
	
	@Override
	public List<Player> getPlayers(){
		throw new RuntimeException("Not implemented");
	}
}
